package main.java.Exam.ExamSolutionOne;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int q = in.nextInt();

        HashSet<String> keyWord = new HashSet<>();

        for(int i = 0;i<q;i++){
            keyWord.add(in.next());
        }

        ArrayList<Product> products = new ArrayList<>();
        ArrayList<ProductCount> rs;

        for(int i = 0;i<n;i++){
            String name = in.next();
            int m = in.nextInt();
            Product product = new Product();
            for(int k = 0;k<m;k++){
                product.words.add(in.next());
            }
            product.n = m;
            product.name = name;

            products.add(product);
        }
        rs = solution(products,keyWord);

        rs.sort((o1, o2) -> o2.count - o1.count);
        for (ProductCount r : rs) {
            System.out.println(r.name);
        }


    }

    private static ArrayList<ProductCount> solution(ArrayList<Product> products,HashSet<String> keyWords){
        int count = 0;
        ArrayList<ProductCount> rs = new ArrayList<>();
        for(Product product:products){
            ArrayList<String> words = product.words;
            for(String w:words){
                if(keyWords.contains(w)){
                    count++;
                }
            }

            rs.add(new ProductCount(product.name,count));
            count = 0;
        }
        return rs;
    }

    static class Product{
        String name;
        Integer n;
        ArrayList<String> words = new ArrayList<>();
    }

    static class ProductCount{
        public ProductCount(String name,int count){
            this.name = name;
            this.count = count;
        }
        String name;
        int count;
    }


}
